﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_PRODUCT.Product.PartData
{
    public partial class Frm_PartMaterialEdit : DevExpress.XtraEditors.XtraForm
    {
        ClsPartDataTable obj = new ClsPartDataTable();
        ClsPartDataDAO clspartDAO = new ClsPartDataDAO();
        DataTable dt;
        public EventHandler Re_Load;

        public string ID;
        public string InputDate;
        public string PartID;
        public string MaterialID;
        public string MaterialType;
        public string Workpiecesize;
        public string shape;
        public string Thickness;
        public string width;
        public string lenght;
        public string Cut;
        public  string RawMachine ;
        public string HandFinish;
        public string HairLine;
        public string WAnod;
        public string BAnod;
        public string Blast30;
        public string Blast60;
        public string Seal;
        public string Migaki;
        public string Bafu;
        public string Cleanwave;
        public string VacPac;
        public string Helisert;
        public string SerialNo;
        public string PalCoat;
        public string Otherpro;
        public string Price;
        public string Memo;
        public string Note;
        public string Caciras;
        public string Inside;
        private LogUtils log;

        public Frm_PartMaterialEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Frm_PartMaterialEdit_Load(object sender, EventArgs e)
        {
            // lấy danh sách PartID truyền vào cbPartID
            dt = clspartDAO.GetAllPartID();
            cbPartID.DataSource = dt;
            cbPartID.DisplayMember = "PartNo";
            cbPartID.ValueMember = "PartNo";

            //Lấy danh sách MaterialID truyền vào cbMaterialID
            dt = clspartDAO.MaterialID();
            cbMaterialID.DataSource = dt;
            cbMaterialID.DisplayMember = "MaterialID";
            cbMaterialID.ValueMember = "MaterialID";

            // lấy các giá trị điền vào các hộp text để sửa
             dateInputDate.Value = DateTime.Parse(InputDate);
             cbPartID.Text =PartID;
             cbMaterialID.Text = MaterialID;
             txtLenght.Text = lenght;
             txtMaterialType.Text = MaterialType;
             txtMemo.Text = Memo;
             txtNote.Text =Note;
             txtOtherpro.Text = Otherpro;
             txtPrice.Text = Price;
             txtShape.Text = shape;
             txtThickness.Text = Thickness;
             txtWidth.Text = width;
             txtWorkpiecesize.Text = Workpiecesize;
             checkBafu.Checked =(!Bafu.Equals(string.Empty))? bool.Parse(Bafu):false;
             checkBAnod.Checked = (!BAnod.Equals(string.Empty)) ? bool.Parse(BAnod) : false;
             checkBlast30.Checked = (!Blast30.Equals(string.Empty)) ? bool.Parse(Blast30) : false;
             checkBlast60.Checked = (!Blast60.Equals(string.Empty)) ? bool.Parse(Blast60) : false;
             checkCaciras.Checked = (!Caciras.Equals(string.Empty)) ? bool.Parse(Caciras) : false;
             checkCleanwave.Checked = (!Cleanwave.Equals(string.Empty)) ? bool.Parse(Cleanwave) : false;
             checkCut.Checked = (!Cut.Equals(string.Empty)) ? bool.Parse(Cut) : false;
             checkHairLine.Checked = (!HairLine.Equals(string.Empty)) ? bool.Parse(HairLine) : false;
             checkHandFinish.Checked = (!HandFinish.Equals(string.Empty)) ? bool.Parse(HandFinish) : false;
             checkHelisert.Checked = (!Helisert.Equals(string.Empty)) ? bool.Parse(Helisert) : false;
             checkInside.Checked = (!Inside.Equals(string.Empty)) ? bool.Parse(Inside) : false;
             checkMigaki.Checked = (!Migaki.Equals(string.Empty)) ? bool.Parse(Migaki) : false;
             checkPalCoat.Checked = (!PalCoat.Equals(string.Empty)) ? bool.Parse(PalCoat) : false;
             checkRawMachine.Checked = (!RawMachine.Equals(string.Empty)) ? bool.Parse(RawMachine) : false;
             checkSeal.Checked = (!Seal.Equals(string.Empty)) ? bool.Parse(Seal) : false;
             checkSeriaNo.Checked = (!SerialNo.Equals(string.Empty)) ? bool.Parse(SerialNo) : false;
             checkVacPac.Checked = (!VacPac.Equals(string.Empty)) ? bool.Parse(VacPac) : false;
             checkWAnod.Checked = (!WAnod.Equals(string.Empty)) ? bool.Parse(WAnod) : false;
        }

        void ThongBao(string name)
        {
            MessageBox.Show("Chưa nhập " + name + ". Vui lòng nhập lại");
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

            if (txtLenght.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.lenght);
            }
            //else if (txtMaterialType.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.MaterialType);
            //}
            //else if (txtMemo.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.Memo);
            //}
            //else if (txtNote.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.Note);
            //}
            //else if (txtOtherpro.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.Otherpro);
            //}
            //else if (txtPrice.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.Price);
            //}
            //else if (txtShape.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.shape);
            //}
            else if (txtThickness.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.Thickness);
            }
            else if (txtWidth.Text == "")
            {
                ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.width);
            }
            //else if (txtWorkpiecesize.Text == "")
            //{
            //    ThongBao(clsConstantsdb.Entities.ClsDBPartData.FieldName.Workpiecesize);
            //}
            else
            {
                try
                {
                    float.Parse(txtLenght.Text);
                    decimal.Parse(txtPrice.Text);
                    float.Parse(txtThickness.Text);
                   // float.Parse(txtWidth.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("Lenght, Price, Thickness, Width phải là số");
                    return;
                }

                try
                {


                    obj.InputDate = dateInputDate.Value;
                    obj.PartID = cbPartID.Text;
                    obj.MaterialID = cbMaterialID.Text;
                    obj.Lenght = float.Parse(txtLenght.Text);
                    obj.MaterialType = txtMaterialType.Text;
                    obj.Memo = txtMemo.Text;
                    obj.Note = txtNote.Text;
                    obj.Otherpro = txtOtherpro.Text;
                    obj.Price = decimal.Parse(txtPrice.Text);
                    obj.Shape = txtShape.Text;
                    obj.Thickness = float.Parse(txtThickness.Text);
                    obj.Width = txtWidth.Text;
                    obj.Workpiecesize = txtWorkpiecesize.Text;
                    obj.Bafu = checkBafu.Checked;
                    obj.BAnod = checkBAnod.Checked;
                    obj.Blast30 = checkBlast30.Checked;
                    obj.Blast60 = checkBlast60.Checked;
                    obj.Caciras = checkCaciras.Checked;
                    obj.Cleanwave = checkCleanwave.Checked;
                    obj.Cut = checkCut.Checked;
                    obj.HairLine = checkHairLine.Checked;
                    obj.HandFinish = checkHandFinish.Checked;
                    obj.Helisert = checkHelisert.Checked;
                    obj.Inside = checkInside.Checked;
                    obj.Migaki = checkMigaki.Checked;
                    obj.PalCoat = checkPalCoat.Checked;
                    obj.RawMachine = checkRawMachine.Checked;
                    obj.Seal = checkSeal.Checked;
                    obj.SerialNo = checkSeriaNo.Checked;
                    obj.VacPac = checkVacPac.Checked;
                    obj.WAnod = checkWAnod.Checked;
                    obj.ID = int.Parse(ID);

                    if (clspartDAO.UpdatePartData(obj) > 0)
                    {

                        //write Log
                        log.Table = LogConstant.Table.Product.PartMaterial;
                        log.Task = LogConstant.Task.Update;
                        log.Description = "PartID: " + obj.PartID + "; InputDate: " + obj.InputDate + "; ID: " + obj.ID;
                        log.WriteLog();

                        MessageBox.Show("Đã cập nhật xong");

                    }
                    else
                    {
                        MessageBox.Show("Xảy ra lỗi khi sửa Hãy thử lại.");
                    }
                    //this.Re_Load(sender, e);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }


        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
    }
}